package com.controller;

import com.mapper.ReportMapper;
import com.pojo.Report;
import com.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    ReportMapper rm;

    /**
     * 获取周报列表
     * @param teacher_id 教师 id
     * @param page 页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getReportList")
    public Map<String,Object> getReportList(Integer teacher_id,Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (teacher_id == null) {
            code = 500;
            msg = "教师 id 不能为空";
        } else {
            if (page == null) page = 0;
            if (pagesize == null) pagesize = 10;
            page *= pagesize;
            map.put("data",rm.getReportList(teacher_id, page, pagesize));
            map.put("total",rm.getTotal(teacher_id));
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 教师评价
     * @param id 周报 id
     * @param score 评分
     * @param remark 批阅评语
     */
    @RequestMapping("/remark")
    public Map<String,Object> remark(Integer id,Double score,String remark) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "ID 不能为空";
        } else {
            Report report = rm.getById(id);
            if (report == null) {
                code = 501;
                msg = "周报不存在";
            } else if (report.getState() != 0) {
                code = 502;
                msg = "周报已评论";
            } else if (score == null) {
                code = 503;
                msg = "评分不能为空";
            } else if (Tool.isNull(remark)) {
                code = 504;
                msg = "批阅评语不能为空";
            } else rm.remark(id, score, remark);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取我的周报
     * @param open_id 学生 id
     * @param page 页码
     * @param pagesize 单页数据量
     */
    @RequestMapping("/getMyReport")
    public Map<String,Object> getMyReport(String open_id,Integer page,Integer pagesize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(open_id)) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (rm.getStudentById(open_id) == null) {
            code = 501;
            msg = "学生 id 不存在";
        } else {
            if (page == null) page = 0;
            if (pagesize == null) pagesize = 10;
            page *= pagesize;
            map.put("data",rm.getStudentReports(open_id, page, pagesize));
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取周报详情
     * @param id 周报 id
     */
    @RequestMapping("/getDetails")
    public Map<String,Object> getDetails(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "周报 id 不能为空";
        } else {
            Report report = rm.getById(id);
            if (report == null) {
                code = 501;
                msg = "周报不存在";
            } else map.put("data",report);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 发布周报
     * @param student_id 学生 id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param subTime 提交时间
     * @param conents 周报内容
     * @param problem 工作中存在的问题
     */
    @RequestMapping("/add")
    public Map<String,Object> add(String student_id, String startTime, String endTime, String subTime, String conents, String problem) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(student_id)) {
            code = 500;
            msg = "学生 id 不能为空";
        } else if (Tool.isNull(startTime)) {
            code = 501;
            msg = "开始时间不能为空";
        } else if (Tool.isNull(endTime)) {
            code = 502;
            msg = "结束时间不能为空";
        } else if (Tool.isNull(subTime)) {
            code = 503;
            msg = "提交时间不能为空";
        } else if (Tool.isNull(conents)) {
            code = 504;
            msg = "周报内容不能为空";
        } else if (Tool.isNull(problem)) {
            code = 505;
            msg = "工作中存在的问题不能为空";
        } else rm.add(student_id, startTime, endTime, subTime, conents, problem);

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

}
